// pages/dicTest/dicTest.js
const util = require('../../utils/util.js')
const {initRes} = require('../../utils/setting.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dics:[],
    curIdx:0,
    testRes:0,//测试结果
    testNums:0,//测试单词数
    testType:"",//测试类型
    trueNumber:0,//正确个数
    clickId:-1,
    trueIdx:-1,
    doneNums:0,
    trueNums:0,
    time:"",
    percent:0,
    trueArr:[],
    falseArr:[]
  },
  
  //点击选择
  tapName2(event){
    let that = this
    for(let i=0; i<this.data.dics[this.data.curIdx].trans.length; i++){
      if(this.data.dics[this.data.curIdx].trans&&this.data.dics[this.data.curIdx].trans[i]===this.data.dics[this.data.curIdx].res){
        this.setData({
          trueIdx:i
        })
      }
    }
    if(event.target.id.substring(1)===this.data.dics[this.data.curIdx].res){
      this.data.trueArr.push(this.data.dics[this.data.curIdx])
      this.setData({
        trueNumber: this.data.trueNumber+1,
        testRes: this.data.testRes+Math.ceil(initRes[this.data.testType]/(this.data.testNums*1)),
      }) 
    } else {
      this.data.falseArr.push(this.data.dics[this.data.curIdx])
    }
    this.setData({
      clickId: Array.from(event.target.id).shift()*1,
    })

    //防抖函数
    function fend(){
      that.setData({
        clickId:-1,
        trueIdx:-1,
        curIdx: that.data.curIdx+1,
        doneNums: that.data.doneNums+1,
        percent: Math.round((that.data.doneNums+1)/that.data.dics.length*100)
      })
    }
    setTimeout(fend,500)
  },

  //返回首页
  backHome(){
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('someEvent', {data: this.data.testRes});
    wx.navigateBack({
      url: '../index/index',
    })
  },

  //详情页面
  goDetail(){
    let that = this
    // console.log(that.data.lastRes);
    wx.navigateTo({
      url: '../detail/detail',
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromTextPage', { trueArr: that.data.trueArr ,falseArr:that.data.falseArr})
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    const eventChannel = this.getOpenerEventChannel()

    eventChannel.on('acceptDataFromIndexPage', function(data) {
      that.setData({
        testNums: data.nums,
        testType: data.type
      })

      // 测试单词加载
      wx.request({
        // url: 'http://152.136.185.210:7878/api/m5/home/multidata', //仅为示例，并非真实的接口地址
      // url: 'http://localhost:8090/test2', //仅为示例，并非真实的接口地址
      url: 'https://book.yueying.cool:18082/test2',//线上地址
        data: {
          num: that.data.testNums,
          type: that.data.testType
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
          console.log(res.data)
          that.setData({
            dics: res.data
          })
        }
      })
      
    })

    this.tapName=util.debounce(this.tapName2,0)
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点分享
   */
  onShareAppMessage: function () {
    let value = this.data.testRes
    let url = `pages/share/share?id=1&value=${value}`
    return {
      title: '查看我的词汇量测试结果',
      path: url,
      imageUrl: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgss0.baidu.com%2F-4o3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2Fb2de9c82d158ccbf887344b418d8bc3eb0354175.jpg&refer=http%3A%2F%2Fgss0.baidu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625965073&t=156b87027a34b898330f87548df68a79",
    }
  }
})